//
//  ZStatusOriginalView.swift
//  swift-weibo
//
//  Created by zero on 17/3/25.
//  Copyright © 2017年 zero. All rights reserved.
//

import UIKit

import SnapKit
import SDWebImage

class ZStatusOriginalView: UIView {

    // 记录原创微博的底部约束
    var originalViewBottomConstraint: Constraint?
    
    var homeModelViewModel:ZHomeModelViewModel?{
        
        didSet{
            
            //  头像
            imgIcon.sd_setImage(with: homeModelViewModel?.profile_image_url, placeholderImage: UIImage(named: "avatar_default"))
            //  赋值
            labName.text = homeModelViewModel?.homeModel?.user?.name ?? "阿拉叫狗蛋"
            //  等级
            imgMbrank.image = homeModelViewModel?.imgMbrank
            //  认证
            imgVerified.image = homeModelViewModel?.imgVerified
            
            labContent.attributedText = homeModelViewModel?.originalContentString
            
            //  移除约束
            originalViewBottomConstraint?.deactivate()
            
            if let picUrls = homeModelViewModel?.homeModel?.pic_urls,picUrls.count > 0{
                
                pictureView.picUrls = picUrls
                
                //  设置约束
                
                self.snp.makeConstraints({ (make) in
                    
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp.bottom).offset(8).constraint
                })
                
                pictureView.isHidden = false
            }else{
                
                self.snp.makeConstraints({ (make) in
                    
                    originalViewBottomConstraint = make.bottom.equalTo(labContent.snp.bottom).offset(8).constraint
                })
                
                pictureView.isHidden = true
            }
            
            
            labTime.text = homeModelViewModel?.timeString
            
            labSource.text = homeModelViewModel?.sourceString
        }
        
        
    }

    
    func getEmojiText(contentText:String?) -> NSMutableAttributedString?{
        
        guard let targetString = contentText  else {
        
            print("字符串为空")
            return nil
        }
        
        let mutableRes = try! NSRegularExpression(pattern: "\\[[\\w]+\\]", options: []).matches(in: targetString, options: [], range: NSRange(location: 0, length: targetString.characters.count))
        
        //  创建可变的富文本
        let attMutableString = NSMutableAttributedString(string: targetString)
        
        //  遍历结果集
        for res in mutableRes.reversed(){
            
            let str = (targetString as NSString).substring(with: res.range)
            
            guard let emojiModel = ZBundleTools.share.getModelFromString(res: str) else {
                
                return nil
                
            }
            
            let imagePath = emojiModel.allPath ?? ""
            
            guard let image = UIImage(named: imagePath, in: ZBundleTools.share.getPath(), compatibleWith: nil) else {
                
                continue
            }
            
            let lineHeigh:CGFloat = 14
            
            let att = NSTextAttachment()
            
            att.image = image
            att.bounds = CGRect(x: 0, y: 0, width: lineHeigh, height: lineHeigh)
            
            //  创建不可变的富文本
            let attrString = NSAttributedString(attachment: att)
            
            //  替换
            attMutableString.replaceCharacters(in: res.range, with: attrString)
            
        }
        
        return attMutableString
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //  布局
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  布局
    private func setupUI(){
        //  头像
        addSubview(imgIcon)
        
        //  约束
        imgIcon.snp.makeConstraints { (make) in
            
            make.top.leading.equalTo(self).offset(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        //  名字
        addSubview(labName)
        
        //  约束
        labName.snp.makeConstraints { (make) in
            
            make.top.equalTo(imgIcon)
            make.leading.equalTo(imgIcon.snp.trailing).offset(8)
        }
        
        //  vip
        addSubview(imgMbrank)
        
        imgMbrank.snp.makeConstraints { (make) in
            make.centerY.equalTo(labName)
            make.leading.equalTo(labName.snp.trailing).offset(8)
        }
        
        //  添加时间
        addSubview(labTime)
        
        labTime.snp.makeConstraints { (make) in
            make.bottom.equalTo(imgIcon)
            make.leading.equalTo(imgIcon.snp.trailing).offset(8)
        }
        
        //  添加来源
        
        addSubview(labSource)
        
        labSource.snp.makeConstraints { (make) in
            
            make.centerY.equalTo(labTime)
            
            make.leading.equalTo(labTime.snp.trailing).offset(8)
        }
        
        //  添加认证
        addSubview(imgVerified)
        
        imgVerified.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(imgIcon.snp.trailing)
            make.centerY.equalTo(imgIcon.snp.bottom)
        }
        
        //  添加微博正文
        addSubview(labContent)
        
        labContent.numberOfLines = 0
        labContent.preferredMaxLayoutWidth = 355
        labContent.snp.makeConstraints { (make) in
            
            make.top.equalTo(imgIcon.snp.bottom).offset(8)
            make.leading.equalTo(imgIcon)
//            make.bottom.equalTo(self)
        }
        
        addSubview(pictureView)
        
        pictureView.backgroundColor = UIColor.white
        //  添加图片
        pictureView.snp.makeConstraints { (make) in
            
            make.left.equalTo(8)
            make.top.equalTo(labContent.snp.bottom).offset(8)
        }
        
        self.snp.makeConstraints { (make) in
            
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(8).constraint
        }
        
    }
    
    //  懒加载空间
    private lazy var imgIcon = UIImageView(image: UIImage(named: "avatar_default"))
    
    private lazy var labName = UILabel (title: "阿拉叫狗蛋", fontColor: UIColor.darkGray, fontSize: 14)
    
    private lazy var imgMbrank = UIImageView(image: (UIImage (named: "common_icon_membership")))
    
    private lazy var labTime = UILabel(title: "微博时间", fontColor: UIColor.orange, fontSize: 10)
    
    private lazy var labSource = UILabel(title: "微博来源", fontColor: UIColor.black, fontSize: 10)
    
    private lazy var imgVerified = UIImageView(image: UIImage(named: "avatar_vgirl"))
    
    private lazy var labContent:YYLabel = {
       
        //  完全控制
        let modifier = YYTextLinePositionSimpleModifier()
        modifier.fixedLineHeight = 22
        
        let lab = YYLabel()
        
        lab.linePositionModifier = modifier
        
        return lab
    }()
    
    private lazy var pictureView:ZStatusPictureView = ZStatusPictureView()

}
